package com.woniu.service.impl;

import com.woniu.outlet.dao.UserMapper;
import com.woniu.outlet.po.User;
import com.woniu.outlet.po.UserExample;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    public void doUpdateUserPerms(Integer userid,String permsids){
        userMapper.deletePermsByUserid(userid);
        String[] permsidsArr = permsids.split(",");
        for(String pid : permsidsArr){
            userMapper.addUserPerms(userid,Integer.parseInt(pid));
        }
    }


    public List<User> getUserList(String username) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(username)){
//            criteria.andUsernameEqualTo(username);
            criteria.andUsernameLike("%"+username+"%");
        }
        return userMapper.selectByExample(example);
    }


    public void deleteUserById(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }


    public void addUser(User user) {
        userMapper.insert(user);
    }


    public void updateUser(User user) {
        userMapper.updateByPrimaryKey(user);
    }


    public List<Integer> getPermsidsByUserid(Integer userid) {
        List<Integer> permsid = userMapper.selectPermsidByUserid(userid);
        return permsid;
    }


    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserExample example = new UserExample();
        //创建一个条件
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        //查询用户
        List<User> userList = userMapper.selectByExample(example);
        if (userList == null || userList.size() == 0) {
            return null;
        } else {
            //创建一个UserDetails实例
            User user = userList.get(0);

            List<String> perCodes = userMapper.selectPercodeByPerm(user.getUsername());
            //创建存放权限的集合 --暂时为空集合
            List<GrantedAuthority> authorities = new ArrayList<>();

            ValueOperations ops =  redisTemplate.opsForValue();
            ops.set(user.getUsername(),perCodes);

            perCodes.forEach(percode->{
                authorities.add(new SimpleGrantedAuthority(percode));
            });

            UserDetails userDetails = null;
            if (user.getStatus().equals("0")){
                userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(),
                        user.getPassword(),true,true,true,
                        user.getStatus().equals("1")?false:true,authorities);
            }else{
                userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(),
                        user.getPassword(),authorities);
            }


            //根据数据库查询信息，封装UserDetails对象，并返回
//            UserDetails userDetails =
//                    new org.springframework.security.core.userdetails.User(user.getUsername(),
//                            user.getPassword(),true,true,true,
//                            user.getStatus().equals("1")?false:true,authorities);
            return userDetails;
        }
    }
}
